package com.guodi.map.controller;

import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapScene;
import com.guodi.map.service.IMapCommonService;
import com.guodi.map.service.IMapSceneConfigService;
import com.guodi.map.service.IMapSceneService;
import com.guodi.map.utils.VerificationUtil;
import com.guodi.map.vo.ApiMapScene;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * @author pwm
 */
@Transactional
@Validated
@RestController
@RequestMapping("/MapScene")
@Api(value = "API - MapSceneController", tags = "三维场景模块表")
public class MapSceneController extends BaseController {

    @Autowired
    private IMapSceneService mapSceneService;
    @Autowired
    private IMapSceneConfigService mapSceneConfigService;
    @Autowired
    private IMapCommonService commonService;

    /**
     * 根据实体属性查询
     *
     * @param mapScene mapScene
     * @return R
     */
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性查询")
    @ApiImplicitParams(@ApiImplicitParam(name = "name", value = "场景名称", required = false, dataType = "string", paramType = "query"))
    public R<?> listByEntity(@ApiIgnore MapScene mapScene) {
        List<MapScene> list = mapSceneService.listByEntity(mapScene);
        return R.data(list);
    }

    /**
     * 根据主键查询
     *
     * @param id id
     * @return R
     */
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R<?> findById(String id) {
        if (StringUtils.isBlank(id)) {
            return R.fail(StatusCode.ERROR_CODE_40005, "id不能为空");
        }
        MapScene mapScene = mapSceneService.getById(id);
        return R.data(mapScene);
    }

    /**
     * 新增场景
     *
     * @param apiMapScene apiMapScene
     * @return R
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public R<?> save(@Valid ApiMapScene apiMapScene) {
        MapScene mapScene = new MapScene();
        BeanUtils.copyProperties(apiMapScene, mapScene);
        if (!prepareData(mapScene, true)) {
            return R.fail(StatusCode.ERROR_CODE_40005, "该场景编码已经存在，请更换！");
        }
        mapScene.setId(UuidUtil.get32UUID());
        if (null == mapScene.getSort()) {
            //设置排序
            Integer sort = commonService.getMaxSort(null, "map_scene");
            mapScene.setSort(sort + 1);
        }
        mapSceneService.save(mapScene);
        // 新增场景配置
        mapSceneConfigService.saveOne(mapScene.getId());
        return R.data(mapScene);
    }

    /**
     * 修改
     *
     * @param mapScene mapScene
     * @return R
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R<?> update(MapScene mapScene) {
        if (!prepareData(mapScene, false)) {
            return R.fail(StatusCode.ERROR_CODE_40005, "该场景编码已经存在，请更换！");
        }
        mapSceneService.updateById(mapScene);
        return R.data(mapScene);
    }

    /**
     * 根据主键删除
     *
     * @param id id
     * @return R
     */
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R<?> delete(String id) {
        if (StringUtils.isBlank(id)) {
            return R.fail(StatusCode.ERROR_CODE_40005, "该场景id不能为空！");
        }
        mapSceneService.removeById(id);
        //删除关联数据
        mapSceneConfigService.removeBySceneId(id);
        return R.data(null);
    }

    private boolean prepareData(MapScene mapScene, boolean isSave) {
        //验证是否重名
        List<MapScene> list = (List<MapScene>) mapSceneService.listByMap(VerificationUtil.verifyMapByCode(mapScene.getCode(), null));
        if (isSave) {
            if (list.size() > 0) {
                return false;
            }
            mapScene.setCreator(getUser().getUserId().toString());
            mapScene.setCreateTime(new Date());
            //防止误修改
            mapScene.setModifier(null);
            mapScene.setModifyTime(null);
            return true;
        } else {
            if (list.size() > 0 && !list.get(0).getId().equals(mapScene.getId())) {
                return false;
            }
            mapScene.setModifier(getUser().getUserId().toString());
            mapScene.setModifyTime(new Date());
            //防止误修改
            mapScene.setCreator(null);
            mapScene.setCreateTime(null);
            return true;
        }
    }
}
